package com.app.logger.controller;

import com.app.core.mvc.result.Result;
import com.app.core.mvc.serialization.argument.DecryptPathVariable;
import com.app.core.mvc.serialization.argument.GetRequestBody;
import com.app.core.util.PojoHelper;
import com.app.logger.entity.Logger;
import com.app.logger.pojo.request.LoggerRequest;
import com.app.logger.pojo.response.LoggerResponse;
import com.app.logger.service.LoggerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "日志接口")
@RestController
@RequestMapping("/v1/loggers")
@Slf4j
@RequiredArgsConstructor
public class LoggerController {

    private final LoggerService service;

    @GetMapping(value = "/page/{current}/{size}")
    @ApiOperation("分页查询")
    public Result<List<LoggerResponse>> basePage(
            @ApiParam(required = true, value = "当前页", example = "1") @PathVariable final Integer current,
            @ApiParam(required = true, value = "每页大小", example = "10") @PathVariable final Integer size,
            @GetRequestBody final LoggerRequest logRequest
    ) {
        IPage<Logger> page = service.page(new Page<>(current, size), new QueryWrapper<>(PojoHelper.convert(logRequest, Logger.class)));
        return Result.success(page, LoggerResponse.class);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation("详情查询")
    public Result<LoggerResponse> baseOne(@ApiParam(required = true, value = "Long", example = "1") @DecryptPathVariable final Long id) {
        return Result.success(service.getById(id), LoggerResponse.class);
    }
}
